/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#ifndef PLIRQ_MANAGER_H
#define PLIRQ_MANAGER_H

#include <machine/pc/processor/irqhandler.h>
#include <cstddef>

/**
 * This class handles IRQ (un)registration.
 */
class PLIrqManager {
public:
	/**
	 * Control codes for the control function.
	 */
	enum ControlCode {
		/**
		 * Controls the number of IRQs within a 1 ms
		 * period that can occur before the IRQ is
		 * mitigated.
		 */
		MitigationThreshold
	};
	/**
	 * Register an ISA irq.
	 *
	 * @param irq the ISA irq number (from 0 to 15).
	 * @param handler pointer to the IrqHandler class.
	 * @param bEdge whether this IRQ is edge triggered or not.
	 * @return the irq's identifier.
	 */
	virtual uint32_t registerIsaIrqHandler(uint8_t irq, PLIrqHandler *handler, bool bEdge = false) = 0;
	/**
	 * Acknoledge the IRQ reception, in case you returned false in the PLIrqHandler::irq() function. If
	 * this is not called there won't be any following irqs.
	 *
	 * @param Id the irq's identifier.
	 */
	virtual void acknowledgeIrq(uint32_t Id) = 0;
	/**
	 * Unregister a previously registered IrqHandler.
	 *
	 * @param Id the irq's identifier.
	 * @param handler pointer to the handler class.
	 */
	virtual void unregisterHandler(uint32_t Id, PLIrqHandler *handler) = 0;
	/**
	 * Enable or disable a irq.
	 *
	 * @param irq The irq to set.
	 * @param enable Enable or disable.
	 */
	virtual void enable(uint8_t irq, bool enable) = 0;
	/**
	 * Called every millisecond, typically handles IRQ mitigation.
	 */
	virtual void tick() {}
	/**
	 * Controls specific elements of a given IRQ.
	 *
	 * @param irq The irq.
	 * @param code The code for the irq.
	 * @param argument The arguments.
	 * @return True if all was successfully.
	 */
	virtual bool control(uint8_t irq, ControlCode code, size_t argument) {
		PLUNUSED(irq)
		PLUNUSED(code)
		PLUNUSED(argument)
		return true;
	}
protected:
	/**
	 * The default constructor.
	 */
	inline PLIrqManager() {}
	/**
	 * The destructor
	 */
	inline virtual ~PLIrqManager() {}
private:
	PLIrqManager(const PLIrqManager &);
	PLIrqManager &operator = (const PLIrqManager &);
};

#endif
